﻿using BJ.AuthDomain.IService;
using BJ.AuthEntity.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DynamicApi;
using System.Threading.Tasks;
using Syspetro.Core.Extensions;

namespace BJ.AuthApp
{
    public class VerifyApp : BaseDynamicApi
    {
        readonly IVerifyService _verify;
        public VerifyApp(IVerifyService verify)
        {
            this._verify = verify;
        }
        /// <summary>
        /// 验证码校验(邮箱)
        /// </summary>
        ///  <param name="verifyCode"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> VerifyEmail(VM_VerifyCode verifyCode)
        {
            return await _verify.VerifyEmail(verifyCode.Account, verifyCode.Code);
        }
        /// <summary>
        /// 验证码校验(手机)
        /// </summary>
        ///  <param name="verifyCode"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> VerifySMS(VM_VerifyCode verifyCode)
        {
            return await _verify.VerifySMS(verifyCode.Account, verifyCode.Code);
        }
        /// <summary>
        /// 邮箱绑定
        /// </summary>
        ///  <param name="verifyCode"></param>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> BindEmail(VM_VerifyCode verifyCode)
        {
            if (!verifyCode.Account.IsEmail())
                return Fail<bool>("邮箱格式不正确");
            return await _verify.BindEmail(verifyCode.Account, verifyCode.Code);
        }
        /// <summary>
        /// 手机绑定
        /// </summary>
        /// <param name="verifyCode"></param>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> BindSMS(VM_VerifyCode verifyCode)
        {
            if (!verifyCode.Account.IsPhone())
                return Fail<bool>("手机号格式不正确");
            return await _verify.BindSMS(verifyCode.Account, verifyCode.Code);
        }
        /// <summary>
        /// 获取邮箱验证(不需要登录)
        /// </summary>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> SendEmail(VM_Code_Email code)
        {
            return await Task.FromResult(_verify.Code_Email(code));
        }
        /// <summary>
        /// 获取邮箱验证(需要登录)
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> SendEmailLogin()
        {
            return await _verify.Code_Email();
        }
        /// <summary>
        /// 获手机验证(不需要登录)
        /// </summary>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> SendSMS(VM_Code_SMS code)
        {
            return await Task.FromResult(_verify.Code_SMS(code));
        }
        /// <summary>
        /// 获取手机验证(需要登录)
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> SendSMSLogin()
        {
            return await _verify.Code_SMS();
        }
        /// <summary>
        /// 是否设置了手机号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> IsSetSMS()
        {
            return Result(await _verify.IsSetSMS());
        }
        /// <summary>
        /// 是否设置了邮箱
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> IsSetEmail()
        {
            return Result(await _verify.IsSetEmail());
        }
        /// <summary>
        /// 是否绑定了Google认证
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public async Task<SpActionResult<bool>> IsSetGoogle()
        {
            return Result(await _verify.IsSetGoogle());
        }
    }
}
